package com.fs.groupSeven.controller;

import com.fs.groupSeven.pojo.College;
import com.fs.groupSeven.service.CollegeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private CollegeService collegeService;

//    跳转院系管理页面
    @RequestMapping("/collegeView")
    public String collegeView(){
        return "collegeManageYRZ/collegeManage";
    }
//获取所有院系的名字
    @RequestMapping("/getcollegename")
    @ResponseBody
    public Map<String,Object> getcollegename(){
        List<String> collegename = collegeService.queryCollegeName();
        Map<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取院系数据成功");
        result.put("collegename",collegename);
        return result;
    }
//获取所有院系的信息
    @RequestMapping("/getlist")
    @ResponseBody
    public Map<String,Object> getList(){
        List<College> collegeSet = collegeService.getAll();
        Map<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取院系数据成功");
        result.put("collegeList",collegeSet);
        return result;
    }

//    获取所有院系的信息并分页
    @RequestMapping("/getlistByPage")
    @ResponseBody
    public Map<String,Object> getlistByPage(Integer currentPage, Integer pageSize){
        PageInfo<College> pageInfo = collegeService.getUsersByPage(currentPage, pageSize);
        System.out.println(pageInfo.getList());
        Map<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取院系数据成功");
        result.put("collegeList",pageInfo.getList());// 返回查询出来的分页数据
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }

//    编辑和添加页面
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(College college){
        Map<String,Object> result = new HashMap<>();
        boolean modified;
        if (college.getCollegeId()>0){
            modified = collegeService.updateOne(college);
        }else {
            modified = collegeService.addOne(college);
        }
        if (!modified){
            result.put("code",-1);
            result.put("message","编辑院系数据失败");
        }else {
            result.put("code",0);
            result.put("message","编辑院系数据成功");
        }
        return result;
    }
//根据id获取某一个院系的信息
    @RequestMapping("/getone")
    @ResponseBody
    public Map<String,Object> getone(Integer collegeId){
        Map<String,Object> result = new HashMap<>();
        College college = collegeService.getOneByUserId(collegeId);
        if (college == null){
            result.put("code",-1);
            result.put("message","编辑院系数据失败");
        }else {
            result.put("code",0);
            result.put("message","编辑院系数据成功");
            result.put("college",college);
        }
        return result;
    }
//根据id删除院系
    @RequestMapping("/deleteone")
    @ResponseBody
    public Map<String,Object> deleteone(Integer collegeId){
        Map<String,Object> result = new HashMap<>();
        boolean delete = collegeService.deleteOneById(collegeId);
        if (!delete){
            result.put("code",-1);
            result.put("message","删除失败");
        }else {
            result.put("code",0);
            result.put("message","删除成功");
        }
        return result;
    }
//    模糊查询
    @RequestMapping("/selectone")
    @ResponseBody
    public Map<String,Object> selectone(String collegename){
        Map<String,Object> result = new HashMap<>();
        Set<College> selectone = collegeService.getUserThatAddressLike(collegename);

        if (selectone==null){
            result.put("code",-1);
            result.put("message","模糊查询失败");
        }else {
            result.put("code",0);
            result.put("selectdd",selectone);
            result.put("message","模糊查询成功");
        }
        return result;
    }
}
